// pages/catalogDetail/catalogDetail.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    banner: [],
    mechanismList: [],
    mechanismGoodList: [],
    voucherCommentList:[],
    latitude: 0.0,
    longitude: 0.0,
    address: '',
    name: '',
    length: 0,
    pageImage:'',
    show:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      id: options.id
    });
    // console.log(that.data.id);
    this.getCatalogDetail()

  },
  /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getCatalogDetail();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  getCatalog: function () {
    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        util.request(api.ServiceIndex, {
          longitude: res.longitude,
          latitude: res.latitude
        }).then(function (res) {
          if (res.errno === 0) {
            that.setData({
              mechanismList: res.data.mechanismList,
              banner: res.data.banner,
              mechanismGoodList: res.data.mechanismGoodList
            });
            console.log(that.picUrls)
          }
        });
      }
    });
  },
  getCatalogDetail: function (id) {
    let that = this;
    util.request(api.GetCatalogDetailList, {
      id: that.data.id
    })
      .then(function (res) {
        // var voucherCommentLists = res.data.voucherCommentList;
        // for (let i = 0; i < voucherCommentLists.length;i++){
        //   voucherCommentLists[i].addTime = voucherCommentLists[i].addTime.substr(0,10);
        //   // voucherCommentLists[i].pic_urls = JSON.parse(voucherCommentLists[i].pic_urls);
        // }
        that.setData({
          currentMechanism: res.data.mechanismList,
          mechanismGoodList: res.data.mechanismGoodList,
          voucherCommentList: res.data.voucherCommentList,
          latitude: parseFloat(res.data.mechanismList.latitude),
          longitude: parseFloat(res.data.mechanismList.longitude),
          address: res.data.mechanismList.address,
          name: res.data.mechanismList.name,
          length: res.data.length
        });
      });
  },
  showImage:function(e){
    console.log(e.target.dataset.src)
    var that = this
    that.setData({
      show:true,
      pageImage: e.target.dataset.src
    })
  },
  noShowImage:function(){
    var that = this
    that.setData({
      show: false
    })
  },
  getVoucherComment:function(){
    let that = this
    util.request(api.GetVoucherCommentList,{
      id: that.data.id
    }).then(function(res){
      that.setData({
        voucherCommentList: res.data.voucherCommentList
      });
    });
  },
  //导航
  navigation: function (e) {
    wx.openLocation({
      scale: 14,
      latitude: this.data.latitude,
      longitude: this.data.longitude,
      name: this.data.name,
      address: this.data.address
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCatalogDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})